package com.xuhf.receiver;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.xuhf.conf.RabbitMQConf;
import com.xuhf.model.CollectMessage;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = RabbitMQConf.GPS_HISTORY_COLLECT_QUEUE, ackMode = "MANUAL", concurrency = "3")
public class GpsHistoryCollectReceiver {

    @RabbitHandler
    public void receive(String message, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        System.out.println("[ " + this.getClass().getSimpleName() + " ] 收到消息：" + message);
        CollectMessage collectMessage = JSONObject.parseObject(message, CollectMessage.class);
        System.out.println("[ " + this.getClass().getSimpleName() + " ] 收到消息实体：" + collectMessage);
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
